<template>
    <div>
        <!-- 面包屑导航区域 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>您当前所在位置</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 卡片视图区域 -->
      <el-card>
         <el-row>
           <el-col :span="24">
             <el-button type="primary" size="mini" @click="addRoles">+新增</el-button>
             <el-button type="primary" size="mini" @click="addMechant">+新增商户</el-button>
           </el-col>
         </el-row>
         <!-- 角色列表区域 -->
         <el-table :data="tableData" height="680" border style="width: 100%">
           <el-table-column prop="title" label="角色名" ></el-table-column>
          <el-table-column prop="tag" label="角色标识" ></el-table-column>
          <el-table-column  label="是否启用">
            <template slot-scope="scope">
              <!-- 0启用，1未启用 -->
              <el-switch v-model="scope.row.del" :inactive-value="1" :active-value="0" @change="statusChange(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="add_time" label="添加时间" sortable></el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="scope">
               <el-button type="primary" size="mini" @click="editBtn(scope.row.id)">编辑</el-button>
            </template>
          </el-table-column>
         </el-table>
         <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[10, 20, 50, 100,]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-card>
    </div>
</template>

<script>
export default {
  data () {
    return {
      // 角色列表
      tableData: [],
      // 请求列表所带参数
      queryInfo: {
        pagenum: 1,
        pagesize: 10
      },
      // 角色列表总条数
      total: 0
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    async getRolesList () {
      const { data: res } = await this.$http.get('/mapi/role', this.queryInfo)
      console.log(res)
      if (res.code !== 200) return this.$message.error('查询角色列表失败！')
      this.$message.success('查询角色列表成功！')
      this.tableData = res.role
      this.total = res.total
    },
    handleSizeChange (val) {
      this.queryInfo.pagesize = val
      this.getRolesList()
    },
    addRoles () {
      this.$router.push('/rolesedit')
    },
    addMechant () {},
    handleCurrentChange (val) {
      console.log(val)
      this.queryInfo.pagenum = val
      this.getRolesList()
    },
    async statusChange (row) {
      console.log(row)

      const info = {
        id: row.id,
        del: row.del
      }
      console.log('info', info)

      const { data: res } = await this.$http.post('mapi/start', info)
      console.log(res)
      if (res.code !== 200) return this.$message.error('修改失败！')
      this.$message.success('修改成功！')
    },
    editBtn (id) {
      sessionStorage.setItem('roleId', id)
      const url = '/rolesedit'
      this.$router.push({
        path: url,
        params: {
          roleId: id
        }
      })
    },
    formatDate (time) {
      const date = new Date(parseInt(time) * 1000)
      const YY = date.getFullYear() + '-'
      const MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      const DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
      const hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
      const mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
      const ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
      return YY + MM + DD + ' ' + hh + mm + ss
    }
  }
}
</script>

<style scoped lang="less">
.el-table{margin: 20px 0;}
</style>
